package org.incubator.numeric;

import java.util.HashSet;
import java.util.Set;

public class IntegerRange {
    private final int min;
    private final int max;

    public IntegerRange(int min, int max) {
        if (min > max) {
            throw new IllegalArgumentException("Minimal value cannot be larger than the maximum.");
        }
        this.min = min;
        this.max = max;
    }

    public static IntegerRange to(int max) {
        return new IntegerRange(0, max);
    }

    public static IntegerRange negative() {
        return new IntegerRange(Integer.MIN_VALUE, 0);
    }

    public static IntegerRange positive() {
        return new IntegerRange(0, Integer.MAX_VALUE);
    }

    public static IntegerRange all() {
        return new IntegerRange(Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    /**
     * Retrieve the start of our range.
     * @return lowest range value
     */
    public int start() {
        return min;
    }

    /**
     * Retrieve the end of our range.
     * @return highest range value
     */
    public int end() {
        return max;
    }

    /**
     * Build a collection of all values inside this integer range.
     * @return range values, as collection
     */
    public Set<Integer> values() {
        Set<Integer> range = new HashSet<Integer>(length());
        for (int i = min; i <= max; i++) {
            range.add(i);
        }
        return range;
    }

    /**
     * Determine the length of this integer range.
     * @return range length
     */
    public int length() {
        return max - min + 1;
    }

}
